package offer;

public class KthLargest_54 {
    int k = 0;
    int res = 0;

    public int kthLargest(TreeNode root, int k) {
        this.k = k;
        if (root == null) return -1;
        build(root);
        return res;

    }

    public void build(TreeNode root) {
        if(root == null)return;
         build(root.right);
        if(k == 0){
            res = root.val;
            return;
        }
        k--;

        build(root.left);
    }


    int k1 = 0;
    int res1 = 0;
    public int kthLargest2(TreeNode root, int k) {
        if(root == null) return 0;
        this.k1 = k;
        build2(root);
        return res1;
    }


    public void build2(TreeNode root) {
        if(root == null){
            return;
        }
        build2(root.right);
        k1--;
        if(k1 == 0){
            res1 = root.val;
            return;
        }
        build2(root.left);


    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
